package com.me.study.beanPostProcessor;

import com.me.study.dao.IndexDao;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.PriorityOrdered;
import org.springframework.stereotype.Component;

/**
 * 自定义 BeanPostProcessor: 对 bean的初始化 增加自定义操作
 *
 * @author ME
 * @date   2019/12/18
 */
@Component
public class TestBeanPostProcessor implements BeanPostProcessor, PriorityOrdered {

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		// 在哪调用的? AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization()
		if (bean instanceof IndexDao) {
			System.out.println("【Spring】 BeanPostProcessor BeforeInitialization: " + beanName);
		}
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		// 在哪调用的? AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization()
		if (bean instanceof IndexDao) {
			System.out.println("【Spring】 BeanPostProcessor AfterInitialization: " + beanName);
		}
		return bean;
	}

	@Override
	public int getOrder() {
		return 101;
	}
}
